import { async } from "q";

export default {
    data() {
        return {
            categoryList:[],
            reqParams:{
                pagenum:1,
                pagesize:5
            },
            total:0,
            addDialogFormVisible:false,
            Rules: {
                cat_name: [{ required: true, message: "分类名称必填", trigger: "blur" }]
            },        
            addForm: {
                cat_pid:0,
                cat_name:"",
                cat_level:0
            },
            valueArr:[],
            cateoptions:[],
            editDialogFormVisible: false,
            editForm: {
                cat_name:""
            },
            

        }
    },
    created() {
        this.getData();
    },
    methods: {
        openEditDialog(row) {
            this.editDialogFormVisible = true;
        },
        handleChange() {

        },
        async openaddDialogFormVisible() {
            const {
                data:{data,meta}
            } = await this.$http.get("categories",{
                params: {type:2}
            })
            if (meta.status !== 200) return this.$message.error("获取分类数据失败");
            this.cateoptions = data;
            this.valueArr = [];
            this.addDialogFormVisible=true;
            this.$nextTick(()=> {
                this.$refs.addForm.resetFields();
            })
        },
        async addCate(){
            let cat_pid = 0;
            if (this.valueArr.length) {
                cat_pid = this.valueArr[this.valueArr.length - 1];
            }
            this.addForm.cat_pid = cat_pid;
            this.addForm.cat_level = this.valueArr.length;
            const {
                data:{meta}
            } = await this.$http.post("categories",this.addForm);
            if (meta.status !== 201) return this.$message.error("添加分类失败");
            this.getData();
            this.addDialogFormVisible = false;
            this.$message.success("添加成功");

        },
        pager(newPage) {
            this.reqParams.pagenum = newPage;
            this.getData();
        },
        async getData() {
            const {
                data:{data,meta}
            } = await this.$http.get("categories",{params:this.reqParams});
            if (meta.status !== 200) return this.$message.error("获取失败");
            this.categoryList = data.result;
            this.total = Math.ceil(data.total / data.pagesize);

        }
    },
}